﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QUT.Bio.Util;

namespace DBLoader {
	/// <summary>
	/// Interaction logic for ConnectionChooser.xaml
	/// </summary>
	public partial class ConnectionChooser: Window {
		private string databaseName = string.Empty;
		private readonly SortedDictionary<string, DatabaseInfo> databases = new SortedDictionary<string, DatabaseInfo>();

		public ConnectionChooser () {
			InitializeComponent();
		}

		public void Init ( DataLayer dataLayer ) {
			dataLayer.ListSilverMapDatabases( delegate( string name, int genomeCount ) {
				databases.Add( name, new DatabaseInfo {
					GenomeCount = genomeCount,
					Name = name
				} );
			} );
			databaseList.ItemsSource = databases.Values;
		}

		private void Ok ( object sender, RoutedEventArgs e ) {
			DialogResult = true;
		}

		private void Cancel ( object sender, RoutedEventArgs e ) {
			DialogResult = false;
		}

		public string DatabaseName {
			get {
				return databaseName;
			}
			set {
				if ( databases.ContainsKey( value ) ) {
					databaseName = value;
				}
				else {
					Alert.Show( "We don't have a SilverMap database called '{0}'", value );
				}
			}
		}

		private class DatabaseInfo {
			public string Name {
				get;
				set;
			}
			public int GenomeCount {
				get;
				set;
			}
		}

		private void databaseListSelectionChanged ( object sender, SelectionChangedEventArgs e ) {
			if ( databaseList.SelectedItem != null ) {
				databaseName = ( (DatabaseInfo) databaseList.SelectedItem ).Name;
			}
		}
	}
}
